#include "ccd290_extract.h"

/* 
** Example contents of inifile / log-file / info-file**

[FITS_HEADER]
EXPOSURE        = 10
N_ROW           = 4700
SERIAL_NUM      = "e2v-CCD290-99-xxxxx"


*/

//  注意！必须要严格按照cfitso的数据类型来定义相关变量！！！

void add_fits_keywords_from_ini( fitsfile *fptr, char *inifile ){

    if( strcmp(inifile,"NULL") == 0 ){
        printf("%s:L%d--> inifile == NULL, so skip adding keywords into fits.\n",__FILE__,__LINE__);
        return;
    }

    int status;

    dictionary *ini;
    ini = iniparser_load(inifile);  // allocate memory for *ini and load keywords & values
    
    /*Write a keyword; must pass the ADDRESS of the value */
    /*
//  1) add a duble    
    double exposure = iniparser_getdouble(ini, "FITS_HEADER:EXPOSURE", 0.5);    // 0.5是人为指定的一个默认值，如果从*ini没有找到关键字EXPOSURE或者它的值，0.5将被赋值给exposure
    fits_update_key(fptr, TDOUBLE, "EXPOSURE", &exposure, "Total Exposure Time", &status);
    */

/*
    目前暂未遇到status不等于0（==0表示正常）的情况
    if( status != 0 ) {
        printf("error ...\n");
    }
*/
/*
//  2) add an integer
    int N_ROW = iniparser_getint(ini, "FITS_HEADER:N_ROW", 4700);
    fits_update_key(fptr, TLONG, "N_ROW", &N_ROW, "TOTAL NUMBER OF ROWS", &status);

//  3) add a string
    char *serial_num = (char*)iniparser_getstring(ini, "FITS_HEADER:SERIAL_NUM", "0x0000");
    fits_update_key(fptr, TSTRING, "SN_NUM", serial_num, "SERIAL NUMBER OF CCD", &status);
*/
    

    /* main part */
    long h_trans = (long)iniparser_getint(ini, "FITS_HEADER:H_TRANS", 1250);
    fits_update_key(fptr, TLONG, "H_TRANS", &h_trans, "NUMBER OF HORIZONTAL TRANSFERS", &status);

    if( status != 0 ){
		    printf("failed to add keyword 'H_TRANS', status = %d\n", status);
		    printf("#debug# h_trans = %ld, status = %d\n", h_trans, status);
		    exit(0);
	  }

    long v_trans = (long)iniparser_getint(ini, "FITS_HEADER:V_TRANS", 4700);
    fits_update_key(fptr, TLONG, "V_TRANS", &v_trans, "NUMBER OF VERTICAL TRANSFERS", &status);

    char *datetag = (char*)iniparser_getstring(ini, "FITS_HEADER:DATETAG", "NULL");
    fits_update_key(fptr, TSTRING, "DATETAG", datetag, "DATETIME ID", &status);

    char *obsstart = (char*)iniparser_getstring(ini, "FITS_HEADER:OBSSTART", "NULL");
    fits_update_key(fptr, TSTRING, "OBSSTART", obsstart, "START OF OBSERVATION (LOCAL TIME)", &status);

    char *obsend = (char*)iniparser_getstring(ini, "FITS_HEADER:OBSEND", "NULL");
    fits_update_key(fptr, TSTRING, "OBSEND", obsend, "END OF OBSERVATION (LOCAL TIME)", &status);

    char *expstart = (char*)iniparser_getstring(ini, "FITS_HEADER:EXPSTART", "NULL");
    fits_update_key(fptr, TSTRING, "EXPSTART", expstart, "START OF EXPOSURE (LOCAL TIME)", &status);

    char *svstart = (char*)iniparser_getstring(ini, "FITS_HEADER:SVSTART", "NULL");
    fits_update_key(fptr, TSTRING, "SVSTART", svstart, "START OF DATA SAVING (LOCAL TIME)", &status);

    double darktime = iniparser_getdouble(ini, "FITS_HEADER:DARKTIME", -99.0);
    fits_update_key(fptr, TDOUBLE, "DARKTIME", &darktime, "DARK TIME IN SECONDS", &status);

    double exptime = iniparser_getdouble(ini, "FITS_HEADER:EXPTIME", -99.0);
    fits_update_key(fptr, TDOUBLE, "EXPTIME", &exptime, "EXPOSURE TIME IN SECONDS", &status);

    char *obstype = (char*)iniparser_getstring(ini, "FITS_HEADER:OBSTYPE", "NULL");
    fits_update_key(fptr, TSTRING, "OBSTYPE", obstype, "TYPE OF OBSERVATION", &status);
    
    double wavelen = iniparser_getdouble(ini, "FITS_HEADER:WAVELEN", -99.0);
    fits_update_key(fptr, TDOUBLE, "WAVELEN", &wavelen, "WAVELENGTH IN NANOMETER", &status);

    double tsys = iniparser_getdouble(ini, "FITS_HEADER:SYSTEMP", -99.0);
    fits_update_key(fptr, TDOUBLE, "SYSTEMP", &tsys, "SYSTEM TEMPERATURE IN KELVIN", &status);

    long imgindex = (long)iniparser_getint(ini, "FITS_HEADER:IMGINDEX", 0);
    fits_update_key(fptr, TLONG, "IMGINDEX", &imgindex, "IMAGE INDEX NUMBER", &status);
    
    long seqnum = (long)iniparser_getint(ini, "FITS_HEADER:SEQNUM", 0);
    fits_update_key(fptr, TLONG, "SEQNUM", &seqnum, "SEQUENCE NUMBER", &status);

    iniparser_freedict(ini);    // free memory of *ini
}
